<?php 
namespace app\mobile\controller;

use think\Controller;
use think\Request;
use think\Db;

class Category extends Controller
{
	public function index()
	{	
		// 遍历全部分类
		$list = $this->cateList();

		 return view('category/index',[
            'title'=>'图书推荐',
            'list'=>$list
        ]);
	}


	//	加载子级书本 
	public function showSon(Request $request)
	{	
		$res = $request->param();
		
		// 处理数据
		$two = db('category')->field(['name','pid'])->find($res['id']);
		$one = db('category')->field(['name','id'])->where('id','=',$two['pid'])->find();
		$res['pid']= $one['id'];

		// 排序
		empty($res['where'])?  $where = 0 : $where= $res['where'];
		switch ($where) {
         case '1':// 人气
             $where = 'hot desc';
             break;
         case '2':// 新书
             $where = 'ctime';
             break;
         default:// 热销
             $where = 'sale desc';
             break;
        }
	
		// 查询书本信息
		$data = db('data')->field(['id','name','price','author','face','tag'])
				->where('cid','=',$res['id'])->order($where)->paginate(20);

		// 分页
		$page = $data->render();

		return view('category/books',[
			'title'=>$one['name'].'_'.$two['name'],
			'one'=>$one['name'],
			'two'=>$two['name'],
			'data'=>$data,
			'page'=>$page,

		]);
	}

	// 遍历全部分类
	public function cateList()
	{
		$list = db('category')->field(['id','name'])->where('id','<','14')->select();
		$res = db('category')->field(['id','pid','name'])->where('pid','>','0')->select();

		foreach ($list as $k => $v) {
			$list[$k]['list'] = [];
			foreach ($res as $ko => $vo) {
				if ($v['id']==$vo['pid']) {
					$vo = array_diff_key($vo,['pid'=>'1']);
					$list[$k]['list'][] = $vo;
				}
			}		
		}
		// echo '<pre>';print_r($list); die;
		return $list;
	}

	// 榜单页
	public function rank()
	{	
		$new = $this->showAll('ctime');
		$search = $this->showAll('search desc');
		$hot = $this->showAll('hot desc');
		$sale = $this->showAll('sale desc');

		 return view('category/rank',[
            'title'=>'榜单',
            'new'=>$new,
            'search'=>$search,
            'hot'=>$hot,
            'sale'=>$sale
        ]);
	}

	// 查看更多
    public function showMore(Request $request)
    {   
        $res = $request->param();
        $where = $res['where'];

        switch ($where) {
         case '1':// 新书
             $data = $this->showAll('ctime',99);
             $name = '新书榜';
             break;
         case '2':// 热搜
             $data = $this->showAll('search desc',99);
             $name = '热搜榜';
             break;
         case '3':// 热门
             $data = $this->showAll('hot desc',99);
             $name = '点击榜';
             break;
         default:// 热销
             $data = $this->showAll('sale desc',99);
             $name = '热销榜';
             break;
        }

         $page = $data->render();
         return view('index/showAll',[
            'title'=>'榜单',
            'name'=>$name,
            'data'=>$data,
            'page'=>$page
        ]);
    }


	// 遍历全部书籍(排序)
	public function showAll($where)
	{
		 $data = Db::field('d.id, d.name, price, author, face, tag')
        ->table(['s75_data'=>'d','s75_category'=>'c'])
        ->where('c.id = d.cid')
        ->order($where)
        ->paginate(20);
        return $data;
	}

	// 免费
	public function free()
	{
		$data = db('data')->field(['id','name','price','author','face','tag'])->order('price')->limit(20)->select();

         return view('category/free',[ 'title'=>'免费','name'=>'限免','data'=>$data]);
	}


}


	






























